<?php

/**
 * LogindummyController
 *
 * @author yukio
 * @version $Id$
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Exception.php';

require_once APPLICATION_PATH . '/models/RequesterModel.php';
require_once APPLICATION_PATH . '/models/ShelterModel.php';
require_once APPLICATION_PATH . '/models/PartyModel.php';
require_once APPLICATION_PATH . '/models/AclModel.php';

class LogindummyController extends Zend_Controller_Action
{
    private $_session;
    private $_role;

    public function init() {
        $this->_session = new Zend_Session_Namespace('okute');
        if (!isset($this->_session->initialized)) {
            Zend_Session::regenerateId();
            $this->_session->initialized = true;
        }

        $this->_session->requester = null;
        $this->_session->shelter = null;
        $this->_session->party = null;
        $this->_session->operator = null;
        
        $this->_redirector = Zend_Registry::get('redirector');
        $this->view->base = $this->getRequest()->getBaseUrl();
    }

    public function indexAction() {
    }

    public function authAction() {
        $role = '';
        $requester_id = '';
        $user = $this->_getParam('user');

        switch ($user) {
            case 'A1':
                $requester_id = 'abc0001';
                $this->_session->role = AclModel::ROLE_REQUESTER;
                break;
            case 'A2':
                $requester_id = 'abc0002';
                $this->_session->role = AclModel::ROLE_REQUESTER;
                break;
            case 'A3':
                $requester_id = 'abc0003';
                $this->_session->role = AclModel::ROLE_REQUESTER;
                break;

            case 'B1':
                $requester_id = 'support1';
                $this->_session->role = AclModel::ROLE_SUPPORTER;
                break;
            case 'B2':
                $requester_id = 'support2';
                $this->_session->role = AclModel::ROLE_SUPPORTER;
                break;

            case 'C1':
                $requester_id = 'carrier1';
                $this->_session->role = AclModel::ROLE_CARRIER;
                break;
            case 'C2':
                $requester_id = 'carrier2';
                $this->_session->role = AclModel::ROLE_CARRIER;
                break;
            case 'D1':
                $requester_id = 'operator1';
                $this->_session->role = AclModel::ROLE_OPERATOR;
                break;
        }

        // 無条件でログイン完了 -- 以下は認証が正しく通った後の処理

        // Cookieを取り扱えない場合に、リダイレクトのURLにセッションIDを付与

        if ($this->_session->role === AclModel::ROLE_REQUESTER) {
            // 要求者インスタンス作成
            $requesterModel = new RequesterModel();
            $this->_session->requester = $requesterModel->fetchRow(
                $requesterModel->select()->where('requester_id = ?', $requester_id));
            if ($this->_session->requester === null) {
                throw new Okute_Exception('Login is required');
            }

            // 避難所のインスタンス作成
            $shelterModel = new ShelterModel();
            $this->_session->shelter = $shelterModel->fetchRow(
                $shelterModel->select()->where('shelter_id = ?',$this->_session->requester->shelter_id));
            if ($this->_session->shelter === null) {
                // 避難所が登録されていない場合はメッセージを出して避難所登録へ飛ぶ
            }
            $this->_redirector->redirectWithSession($this->view->base.'/request');
        }

        if ($this->_session->role === AclModel::ROLE_SUPPORTER ||
            $this->_session->role === AclModel::ROLE_CARRIER )
        {
            // 団体インスタンス作成
            $partyModel = new partyModel();
            $this->_session->party = $partyModel->fetchRow($partyModel->select()->where('party_id = ?', $requester_id));
            $this->_redirector->redirectWithSession($this->view->base.'/support');
        }
        
        if ($this->_session->role === AclModel::ROLE_OPERATOR)
        {
            $this->_redirector->redirectWithSession($this->view->base.'/operator');
        }
    }
    
    public function postDispatch() {
    	Zend_Session::regenerateId();
    }
}